package com.example.test.funinteface;

import javax.xml.ws.handler.LogicalHandler;
import java.util.Objects;

/**
 * @author renyitong
 * @version 1.0.0
 * @createTime 2025年07月21日
 * @Description
 */
public class FunctionUtils {

    public static CustomException ifFalse(boolean condition) {
        return errorMessage -> {
            if (Boolean.FALSE.equals(condition)) {
                throw new RuntimeException(errorMessage);
            }
        };
    }

    public static void main(String[] args) {
        // FunctionUtils.ifFalse(false).throwCustomException("errors");

        // FunctionUtils.isTrueOrFalse(true).logicHandler(()->{
        //     System.out.println("成功啦");
        // }, ()->{
        //     throw new RuntimeException("错误啦");
        // });

        FunctionUtils.isPresent("11").presentOrOtherHandler(obj -> {
            System.out.println("值存在的");
        }, () -> {
            throw new RuntimeException("值不存在!");
        });
    }


    public static LogicHandler isTrueOrFalse(boolean condition) {
        return (trueHandler, falseHandler) -> {
            if (Boolean.FALSE.equals(condition)) {
                falseHandler.run();
            } else {
                trueHandler.run();
            }
        };
    }


    public static PresentOrOtherHandler<?> isPresent(Object obj) {
        return ((action, otherHandler) -> {
            if (Objects.nonNull(obj)) {
                action.accept(obj);
            } else {
                otherHandler.run();
            }
        });
    }

}
